package com.andatsoft.laisim.activity.setting;

import java.util.ArrayList;
import java.util.List;

import net.margaritov.preference.colorpicker.ColorPickerDialog;
import net.margaritov.preference.colorpicker.ColorPickerDialog.OnColorChangedListener;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.CheckBox;
import android.widget.TextView;

import com.andatsoft.laisim.R;
import com.andatsoft.laisim.activity.BaseFragmentActivity;
import com.andatsoft.laisim.adapter.MainPagerAdapter;
import com.andatsoft.laisim.utils.Common;
import com.andatsoft.laisim.utils.ToastMessage;
import com.andatsoft.laisim.utils.Utils;
import com.andatsoft.laisim.view.DotStatusBar;
import com.andatsoft.laisim.view.MediumWidgetFragment;
import com.andatsoft.laisim.view.SmallWidgetFragment;
import com.andatsoft.laisim.view.TinyWidgetFragment;

public class WidgetSettingActivity extends BaseFragmentActivity {

	private ViewPager mViewPager;
	private TextView mTvTitle;
	private TextView mTvSave;
	private TextView mTvBack;
	private View mViewBackgroundColor;
	private View mViewTitleColor;
	private View mViewOthersColor;
	private CheckBox mChkBoxTitleBold;
	private CheckBox mChkBoxTitleItalic;
	private CheckBox mChkBoxOthersBold;
	private CheckBox mChkBoxOthersItalic;
	private DotStatusBar mDotStatusBar;

	private List<Fragment> mFragmentList;
	private int mMode = 0;// 0:tiny, 1:small, 2:medium

	// ///////////////////////////////////////
	// Setting
	private int mTinyBgColor;

	private int mSmallBgColor;
	private int mSmallTitleColor;
	private boolean mSmallTitleBold;
	private boolean mSmallTitleItalic;

	private int mMediumBgColor;
	private int mMediumTitleColor;
	private int mMediumOthersColor;
	private boolean mMediumTitleBold;
	private boolean mMediumTitleItalic;
	private boolean mMediumOthersBold;
	private boolean mMediumOthersItalic;

	// ///////////////////////////////////////

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_setting_widget);
		initViews();
		initPager();
		setupListeners();
		restoreSetting();
		doWhenModeChanged();
	}

	private void initViews() {
		mViewPager = (ViewPager) findViewById(R.id.pager_setting_widget);
		mTvTitle = (TextView) findViewById(R.id.tv_title_setting_widget);
		mTvSave = (TextView) findViewById(R.id.tv_save_setting_widget);
		mTvBack = (TextView) findViewById(R.id.tv_back_setting_widget);

		mViewBackgroundColor = findViewById(R.id.view_bg_color_setting_widget);
		mViewTitleColor = findViewById(R.id.view_title_color_setting_widget);
		mViewOthersColor = findViewById(R.id.view_others_color_setting_widget);

		mChkBoxTitleBold = (CheckBox) findViewById(R.id.chk_box_title_bold_setting_activity);
		mChkBoxTitleItalic = (CheckBox) findViewById(R.id.chk_box_title_italic_setting_activity);
		mChkBoxOthersBold = (CheckBox) findViewById(R.id.chk_box_others_bold_setting_activity);
		mChkBoxOthersItalic = (CheckBox) findViewById(R.id.chk_box_others_italic_setting_activity);

		mDotStatusBar = (DotStatusBar) findViewById(R.id.dot_status_setting_widget);
		mDotStatusBar.setMaxPoint(3);
	}

	private void initPager() {
		Log.e("MainAc", "initpager get called");
		if (mFragmentList == null)
			mFragmentList = new ArrayList<Fragment>();
		if (mFragmentList.size() <= 0) {
			mFragmentList.add(new TinyWidgetFragment());
			mFragmentList.add(new SmallWidgetFragment());
			mFragmentList.add(new MediumWidgetFragment());
		}

		MainPagerAdapter pagerAdapter = new MainPagerAdapter(
				getSupportFragmentManager(), mFragmentList);
		mViewPager.setAdapter(pagerAdapter);
		mTvTitle.setText("Tiny");
		setTextFunctionEnabled(false, false);
	}

	private void setupListeners() {
		mViewPager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int pos) {
				mMode = pos;
				doWhenModeChanged();
				mDotStatusBar.setSelectedPoint(pos);
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {

			}
		});

		mTvBack.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					setAlpha(v, 0.5f);
					return true;
				case MotionEvent.ACTION_UP:
					setAlpha(v, 1.0f);
					WidgetSettingActivity.this.finish();
					return true;
				case MotionEvent.ACTION_CANCEL:
					setAlpha(v, 1.0f);
					WidgetSettingActivity.this.finish();
					return true;
				}
				return false;
			}
		});
		mTvSave.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					setAlpha(v, 0.5f);
					return true;
				case MotionEvent.ACTION_UP:
					setAlpha(v, 1.0f);
					saveSetting();
					return true;
				case MotionEvent.ACTION_CANCEL:
					setAlpha(v, 1.0f);
					return true;
				}
				return false;
			}
		});

		mViewBackgroundColor.setOnTouchListener(new OnTouchListener() {

			@SuppressLint("NewApi")
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					setAlpha(v, 0.5f);
					return true;
				case MotionEvent.ACTION_UP:
					setAlpha(v, 1.0f);
					ColorDrawable drawable = (ColorDrawable) mViewBackgroundColor
							.getBackground();
					int oldColor = 0xffffffff;
					if (Utils.androidOs > Common.ANDROID_OS_GINGERBREAD) {
						oldColor = drawable.getColor();
					}
					ColorPickerDialog colorDialog = new ColorPickerDialog(
							WidgetSettingActivity.this, oldColor);
					colorDialog.setAlphaSliderVisible(true);
					colorDialog
							.setOnColorChangedListener(new OnColorChangedListener() {

								@Override
								public void onColorChanged(int color) {
									mViewBackgroundColor
											.setBackgroundColor(color);
									updateBackgroundColor(color);
								}
							});
					colorDialog.show();
					return true;
				case MotionEvent.ACTION_CANCEL:
					setAlpha(v, 1.0f);
					// WidgetSettingActivity.this.finish();
					return true;
				}
				return false;
			}
		});

		mViewTitleColor.setOnTouchListener(new OnTouchListener() {

			@SuppressLint("NewApi")
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					setAlpha(v, 0.5f);
					return true;
				case MotionEvent.ACTION_UP:
					setAlpha(v, 1.0f);
					ColorDrawable drawable = (ColorDrawable) mViewTitleColor
							.getBackground();
					int oldColor = 0xffffffff;
					if (Utils.androidOs > Common.ANDROID_OS_GINGERBREAD) {
						oldColor = drawable.getColor();
					}
					ColorPickerDialog colorDialog = new ColorPickerDialog(
							WidgetSettingActivity.this, oldColor);
					colorDialog.setAlphaSliderVisible(true);
					colorDialog
							.setOnColorChangedListener(new OnColorChangedListener() {

								@Override
								public void onColorChanged(int color) {
									mViewTitleColor.setBackgroundColor(color);
									doWhenTextColorChanged(color, true);
								}
							});
					colorDialog.show();
					return true;
				case MotionEvent.ACTION_CANCEL:
					setAlpha(v, 1.0f);
					// WidgetSettingActivity.this.finish();
					return true;
				}
				return false;
			}
		});

		mViewOthersColor.setOnTouchListener(new OnTouchListener() {

			@SuppressLint("NewApi")
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					setAlpha(v, 0.5f);
					return true;
				case MotionEvent.ACTION_UP:
					setAlpha(v, 1.0f);
					ColorDrawable drawable = (ColorDrawable) mViewOthersColor
							.getBackground();
					int oldColor = 0xffffffff;
					if (Utils.androidOs > Common.ANDROID_OS_GINGERBREAD) {
						oldColor = drawable.getColor();
					}
					ColorPickerDialog colorDialog = new ColorPickerDialog(
							WidgetSettingActivity.this, oldColor);
					colorDialog.setAlphaSliderVisible(true);
					colorDialog
							.setOnColorChangedListener(new OnColorChangedListener() {

								@Override
								public void onColorChanged(int color) {
									mViewOthersColor.setBackgroundColor(color);
									doWhenTextColorChanged(color, false);
								}
							});
					colorDialog.show();
					return true;
				case MotionEvent.ACTION_CANCEL:
					setAlpha(v, 1.0f);
					// WidgetSettingActivity.this.finish();
					return true;
				}
				return false;
			}
		});

		mChkBoxTitleBold.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				doWhenCheckBoxBoldChanged(mChkBoxTitleBold.isChecked(), true);
			}
		});

		mChkBoxOthersBold.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				doWhenCheckBoxBoldChanged(mChkBoxOthersBold.isChecked(), false);
			}
		});

		mChkBoxTitleItalic.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				doWhenCheckBoxItalicChanged(mChkBoxTitleItalic.isChecked(),
						true);
			}
		});

		mChkBoxOthersItalic.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				doWhenCheckBoxItalicChanged(mChkBoxOthersItalic.isChecked(),
						false);
			}
		});
	}

	private void restoreSetting() {
		SharedPreferences sp = getSharedPreferences(
				Common.SP_WIDGET_SETTING_TINY_NAME, Context.MODE_PRIVATE);
		mTinyBgColor = sp.getInt(Common.SP_WIDGET_SETTING_TINY_BG, 0x00ffffff);

		sp = getSharedPreferences(Common.SP_WIDGET_SETTING_SMALL_NAME,
				Context.MODE_PRIVATE);
		mSmallBgColor = sp
				.getInt(Common.SP_WIDGET_SETTING_SMALL_BG, 0x00ffffff);
		mSmallTitleColor = sp.getInt(
				Common.SP_WIDGET_SETTING_SMALL_TITLE_COLOR, 0xff000000);
		mSmallTitleBold = sp.getBoolean(
				Common.SP_WIDGET_SETTING_SMALL_TITLE_IS_BOLD, false);
		mSmallTitleItalic = sp.getBoolean(
				Common.SP_WIDGET_SETTING_SMALL_TITLE_IS_ITALIC, false);

		sp = getSharedPreferences(Common.SP_WIDGET_SETTING_MEDIUM_NAME,
				Context.MODE_PRIVATE);
		mMediumBgColor = sp.getInt(Common.SP_WIDGET_SETTING_MEDIUM_BG,
				0x00ffffff);
		mMediumTitleColor = sp.getInt(
				Common.SP_WIDGET_SETTING_MEDIUM_TITLE_COLOR, 0xff000000);
		mMediumTitleBold = sp.getBoolean(
				Common.SP_WIDGET_SETTING_MEDIUM_TITLE_IS_BOLD, false);
		mMediumTitleItalic = sp.getBoolean(
				Common.SP_WIDGET_SETTING_MEDIUM_TITLE_IS_ITALIC, false);
		mMediumOthersColor = sp.getInt(
				Common.SP_WIDGET_SETTING_MEDIUM_OTHERS_COLOR, 0xff000000);
		mMediumOthersBold = sp.getBoolean(
				Common.SP_WIDGET_SETTING_MEDIUM_OTHERS_IS_BOLD, false);
		mMediumOthersItalic = sp.getBoolean(
				Common.SP_WIDGET_SETTING_MEDIUM_OTHERS_IS_ITALIC, false);

	}

	private void doWhenModeChanged() {
		switch (mMode) {
		case 0:
			// tiny
			mTvTitle.setText("Tiny");
			setTextFunctionEnabled(false, false);
			mViewBackgroundColor.setBackgroundColor(mTinyBgColor);
			mViewTitleColor.setBackgroundColor(0x00000000);
			mViewOthersColor.setBackgroundColor(0x00000000);
			// update preview
			updateBackgroundColor(mTinyBgColor);
			break;
		case 1:
			// small
			mTvTitle.setText("Small");
			setTextFunctionEnabled(true, false);
			mViewBackgroundColor.setBackgroundColor(mSmallBgColor);
			mViewTitleColor.setBackgroundColor(mSmallTitleColor);
			mViewOthersColor.setBackgroundColor(0x00000000);
			mChkBoxTitleBold.setChecked(mSmallTitleBold);
			mChkBoxTitleItalic.setChecked(mSmallTitleItalic);
			mChkBoxOthersBold.setChecked(false);
			mChkBoxOthersItalic.setChecked(false);

			// update preview
			updateBackgroundColor(mSmallBgColor);
			updateTextViewTitle(mSmallTitleBold, mSmallTitleItalic,
					mSmallTitleColor);
			break;
		case 2:
			// medium
			mTvTitle.setText("Medium");
			setTextFunctionEnabled(true, true);
			mViewBackgroundColor.setBackgroundColor(mMediumBgColor);
			mViewTitleColor.setBackgroundColor(mMediumTitleColor);
			mViewOthersColor.setBackgroundColor(mMediumOthersColor);
			mChkBoxTitleBold.setChecked(mMediumTitleBold);
			mChkBoxTitleItalic.setChecked(mMediumTitleItalic);
			mChkBoxOthersBold.setChecked(mMediumOthersBold);
			mChkBoxOthersItalic.setChecked(mMediumOthersItalic);
			// update preview
			updateBackgroundColor(mMediumBgColor);
			updateTextViewTitle(mMediumTitleBold, mMediumTitleItalic,
					mMediumTitleColor);
			updateTextViewOthers(mMediumOthersBold, mMediumOthersItalic,
					mMediumOthersColor);
			break;
		}
	}

	private void setTextFunctionEnabled(boolean val, boolean othersVal) {
		mChkBoxOthersBold.setEnabled(othersVal);
		mChkBoxOthersItalic.setEnabled(othersVal);
		mViewOthersColor.setEnabled(othersVal);

		mChkBoxTitleBold.setEnabled(val);
		mChkBoxTitleItalic.setEnabled(val);

		mViewTitleColor.setEnabled(val);

	}

	private void updateBackgroundColor(int color) {
		switch (mMode) {
		case 0:
			// tiny
			mTinyBgColor = color;
			((TinyWidgetFragment) mFragmentList.get(0))
					.updateBackground(mTinyBgColor);
			break;
		case 1:
			// small
			mSmallBgColor = color;
			((SmallWidgetFragment) mFragmentList.get(1))
					.updateBackground(mSmallBgColor);
			break;
		case 2:
			// medium
			mMediumBgColor = color;
			((MediumWidgetFragment) mFragmentList.get(2))
					.updateBackground(mMediumBgColor);
			break;
		}
	}

	private void doWhenCheckBoxBoldChanged(boolean value, boolean isChkTitle) {
		switch (mMode) {
		case 1:
			// small
			mSmallTitleBold = value;
			Log.e("small bold", value + "");
			((SmallWidgetFragment) mFragmentList.get(1))
					.updateTextViewBoldItalic(value,
							mChkBoxTitleItalic.isChecked());
			break;
		case 2:
			// medium
			boolean isItalic = false;
			if (isChkTitle) {
				mMediumTitleBold = value;
				isItalic = mChkBoxTitleItalic.isChecked();
			} else {
				mMediumOthersBold = value;
				isItalic = mChkBoxOthersItalic.isChecked();
			}
			((MediumWidgetFragment) mFragmentList.get(2))
					.updateTextViewBoldItalic(value, isItalic, isChkTitle);

			break;
		}
	}

	private void doWhenCheckBoxItalicChanged(boolean value, boolean isChkTitle) {
		switch (mMode) {
		case 1:
			// small
			mSmallTitleItalic = value;
			Log.e("small italic", value + "");
			((SmallWidgetFragment) mFragmentList.get(1))
					.updateTextViewBoldItalic(mChkBoxTitleBold.isChecked(),
							value);
			break;
		case 2:
			// medium
			boolean isBold = false;
			if (isChkTitle) {
				mMediumTitleItalic = value;
				isBold = mChkBoxTitleBold.isChecked();
			} else {
				mMediumOthersItalic = value;
				isBold = mChkBoxOthersBold.isChecked();
			}
			((MediumWidgetFragment) mFragmentList.get(2))
					.updateTextViewBoldItalic(isBold, value, isChkTitle);

			break;
		}
	}

	private void doWhenTextColorChanged(int value, boolean isTitle) {
		switch (mMode) {
		case 1:
			// small
			mSmallTitleColor = value;
			((SmallWidgetFragment) mFragmentList.get(1))
					.updateTextViewColor(value);
			break;
		case 2:
			// medium
			if (isTitle) {
				mMediumTitleColor = value;
			} else {
				mMediumOthersColor = value;
			}
			((MediumWidgetFragment) mFragmentList.get(2)).updateTextViewColor(
					value, isTitle);

			break;
		}
	}

	private void updateTextViewTitle(boolean isBold, boolean isItalic, int color) {
		switch (mMode) {
		case 1:
			// small
			((SmallWidgetFragment) mFragmentList.get(1)).updateTextView(isBold,
					isItalic, color);
			break;
		case 2:
			// medium
			((MediumWidgetFragment) mFragmentList.get(2)).updateTextViewTitle(
					isBold, isItalic, color);
			break;
		}
	}

	private void updateTextViewOthers(boolean isBold, boolean isItalic,
			int color) {
		switch (mMode) {
		case 2:
			// medium
			((MediumWidgetFragment) mFragmentList.get(2)).updateTextViewOthers(
					isBold, isItalic, color);
			break;
		}
	}

	private void saveSetting() {
		SharedPreferences sp;
		switch (mMode) {
		case 0:
			// tiny
			sp = getSharedPreferences(Common.SP_WIDGET_SETTING_TINY_NAME,
					Context.MODE_PRIVATE);
			SharedPreferences.Editor ed = sp.edit();
			ed.putInt(Common.SP_WIDGET_SETTING_TINY_BG, mTinyBgColor);
			ed.commit();
			new ToastMessage(getApplicationContext())
					.showShortMessage(getString(R.string.common_text_save)
							+ " tiny widget");
			break;
		case 1:
			// small
			sp = getSharedPreferences(Common.SP_WIDGET_SETTING_SMALL_NAME,
					Context.MODE_PRIVATE);
			SharedPreferences.Editor edSmall = sp.edit();
			edSmall.putInt(Common.SP_WIDGET_SETTING_SMALL_BG, mSmallBgColor);
			edSmall.putInt(Common.SP_WIDGET_SETTING_SMALL_TITLE_COLOR,
					mSmallTitleColor);
			edSmall.putBoolean(Common.SP_WIDGET_SETTING_SMALL_TITLE_IS_BOLD,
					mSmallTitleBold);
			edSmall.putBoolean(Common.SP_WIDGET_SETTING_SMALL_TITLE_IS_ITALIC,
					mSmallTitleItalic);
			edSmall.commit();
			new ToastMessage(getApplicationContext())
					.showShortMessage(getString(R.string.common_text_save)
							+ " small widget");
			break;
		case 2:
			// medium
			sp = getSharedPreferences(Common.SP_WIDGET_SETTING_MEDIUM_NAME,
					Context.MODE_PRIVATE);
			SharedPreferences.Editor edMed = sp.edit();
			edMed.putInt(Common.SP_WIDGET_SETTING_MEDIUM_BG, mMediumBgColor);
			edMed.putInt(Common.SP_WIDGET_SETTING_MEDIUM_TITLE_COLOR,
					mMediumTitleColor);
			edMed.putInt(Common.SP_WIDGET_SETTING_MEDIUM_OTHERS_COLOR,
					mMediumOthersColor);
			edMed.putBoolean(Common.SP_WIDGET_SETTING_MEDIUM_TITLE_IS_BOLD,
					mMediumTitleBold);
			edMed.putBoolean(Common.SP_WIDGET_SETTING_MEDIUM_TITLE_IS_ITALIC,
					mMediumTitleItalic);
			edMed.putBoolean(Common.SP_WIDGET_SETTING_MEDIUM_OTHERS_IS_BOLD,
					mMediumOthersBold);
			edMed.putBoolean(Common.SP_WIDGET_SETTING_MEDIUM_OTHERS_IS_ITALIC,
					mMediumOthersItalic);
			edMed.commit();
			new ToastMessage(getApplicationContext())
					.showShortMessage(getString(R.string.common_text_save)
							+ " medium widget");
			break;
		}
	}
}
